package com.example.gybicycle.common.util;

import com.example.gybicycle.common.enums.NumType;
import com.example.gybicycle.common.redis.RedisUtil;
import com.example.gybicycle.entity.MemberDeposit;
import com.example.gybicycle.entity.MemberOrder;
import com.example.gybicycle.entity.MemberPay;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;

@Component
public class NumberUtil {
    @Autowired
    RedisUtil redisUtil;
    public static  NumberUtil numberUtil;
    @PostConstruct
    public void init(){
        numberUtil=this;
        numberUtil.redisUtil=this.redisUtil;
    }

    public static String getNum(NumType type){
        String num=type.getKey()+DataUtil.toString(new Date(),"yyyyMMdd");
        String key=type.getKey();
        Integer no=(Integer) numberUtil.redisUtil.get("NO:"+key);
        if(no==null)
            no=1;
        num=num+zeroPad(no,4);
        no++;
        numberUtil.redisUtil.set("NO:"+key,no,DataUtil.toTomorrowScond());
        return num;
    }

    public static String getLockNum(String mainId,String cid){
        String lockId="";
        lockId="LOCK"+mainId;
        if(cid!=null&&cid.length()>0)
            lockId+="D"+NumberUtil.zeroPad(Integer.valueOf(cid),3);
       return lockId;
    }

    /**
     * 补零
     * @param num 数据
     * @param digit 位数
     * @return
     */
    public static String zeroPad(int num,int digit){
        String dig="";
        for(int i=0;i<digit;i++)
            dig+="0";

        DecimalFormat df = new DecimalFormat(dig);
        String str = df.format(num);
        return str;
    }

    public static MemberPay createPay(MemberOrder order,String openid){
        MemberPay pay=new MemberPay();
        pay.setMemberOrderId(order.getId());
        pay.setMemberId(order.getMemberId());
        pay.setPayType(1);
        pay.setPriceType(order.getPriceType());
        pay.setCreateTime(new Date());
//        BigDecimal money=PayUtil.getPrice(order);
        BigDecimal money=new BigDecimal(0.01);
        pay.setMoney(money);
        pay.setPayState(0);
        if(money.equals(new BigDecimal(0))){
            pay.setPayState(1);
            pay.setPayTime(new Date());
        }
        pay.setMemberOpenId(openid);
        pay.setRemark("爱可赢骑行费用");
        return pay;
    }
    public static MemberPay createPay(MemberDeposit order,String openid){
        MemberPay pay=new MemberPay();
        pay.setMemberOrderId(order.getId());
        pay.setMemberId(order.getMemberId());
        pay.setPayType(0);
        pay.setCreateTime(new Date());
//        BigDecimal money=order.getMoney();
        BigDecimal money=new BigDecimal(0.01);
        pay.setMoney(money);
        pay.setPayState(0);
        if(money.equals(new BigDecimal(0))){
            pay.setPayState(1);
            pay.setPayTime(new Date());
        }
        pay.setMemberOpenId(openid);
        pay.setRemark("爱可赢骑行押金支付");
        return pay;
    }

}
